package geektime.spring.data.mybatisdemo;

import geektime.spring.data.mybatisdemo.model.Coffee;
import geektime.spring.data.mybatisdemo.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;


@SpringBootApplication
@Slf4j
@EnableCaching
@EnableTransactionManagement
@MapperScan("geektime.spring.data.mybatisdemo.mapper")
public class MybatisDemoApplication implements ApplicationRunner {
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private JedisPoolConfig jedisPoolConfig;
    @Autowired
    private CoffeeService coffeeService;

    @Bean
    @ConfigurationProperties("redis")
    public JedisPoolConfig jedisPoolConfig() {
        return new JedisPoolConfig();
    }

    @Bean(destroyMethod = "close")
    public JedisPool jedisPool(@Value("${redis.host}") String host) {
        return new JedisPool(jedisPoolConfig(), host);
    }


    public static void main(String[] args) {
        SpringApplication.run(MybatisDemoApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Coffee c = Coffee.builder().name("espresso")
                .price(Money.of(CurrencyUnit.of("CNY"), 20.0)).build();
        query(c);

    }

    public int add(Coffee c){
        int count = coffeeService.save(c);
        log.info("Save {} Coffee: {}", count, c);
        return count;
    }

    public int update(Coffee c){
        c.setName("blacktea");
        int count = coffeeService.update(c);
        log.info("Update {} Coffee: {}", count, c);
        return count;
    }

    public int delete(Coffee c){
        int count = coffeeService.delete(c);
        log.info("Delete {} Coffee: {}", count, c);
        return count;
    }

    public void query(Coffee c) {
        log.info(jedisPoolConfig.toString());
        try (Jedis jedis = jedisPool.getResource()) {
            coffeeService.findByIdWithRowBounds(c.getId(), new RowBounds(1, 3))
                    .forEach(list -> {
                        jedis.hset("springbucks-menu",
                                list.getName(),
                                Long.toString(list.getPrice().getAmountMinorLong()));
                    });


            Map<String, String> menu = jedis.hgetAll("springbucks-menu");
            log.info("Menu: {}", menu);

            String price = jedis.hget("springbucks-menu", "espresso");
            log.info("blacktea - {}",
                    Money.ofMinor(CurrencyUnit.of("CNY"), Long.parseLong(price)));
        }
    }

}

